<template>
  <div class="alarm-testTool">
    <div class="alarm-eqp">
      <div class="lotinfo-tilte">Alarm 设置</div>
      <dynForm
        :info-form="searchForm"
        :search-tags="searchTags.slice(0, 1)"
        :select-list="selectList"
        :rules="rules"
        label-width="120px"
      />
    </div>
    <div class="alarm-base">
      <div class="lotinfo-tilte">Alarm基本信息</div>
      <dynForm
        ref="alarmBaseForm"
        :info-form="searchForm"
        :search-tags="searchTags.slice(1, 8)"
        :select-list="selectList"
        :rules="rules"
        label-width="120px"
      />
    </div>
    <div class="alarm-lotinfo">
      <div class="lotinfo-tilte">Alarm Lots</div>
      <dynForm
        :info-form="searchForm"
        :search-tags="searchTags.slice(8)"
        :select-list="selectList"
        :rules="rules"
        label-width="120px"
      />
      <el-row :style="{padding: '20px', backgroundColor: '#fff'}">
        <el-col :span="24">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="send">发送报警</el-button>
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="cancelSend"
          >取消发送</el-button>
          <!-- <el-button
            type="primary"
            icon="el-icon-edit"
          >Hold Lot</el-button>
          <el-button
            type="primary"
            icon="el-icon-delete"
          >Hold Equipment</el-button> -->
        </el-col>
      </el-row>
    </div>
    <div class="swiper-message">
      <div class="swiper-title">
        <span class="swiper-title-time">时间</span>
        <span class="swiper-title-message">消息</span>
      </div>
      <div class="swiper-content">
        <div :style="messageStyles">
          <div v-for="item in messageList" :key="item.id" class="swiper-message-item">
            <span class="item-time">{{ item.time }}</span>
            <span class="item-message">{{ item.content }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { sendAlarm } from '@/api/alarm/testTool'
import { getFullTime } from '@/utils/pickTime'
var count = 1
export default {
  name: 'AlarmTestTool',
  props: {},
  data() {
    return {
      trackinTime: null,
      searchForm: {
        interval: 1000,
        eqpId: 'ams04',
        alarmFlag: 1,
        alarmId: 'A0001',
        alarmDesc: 'this is alarm description',
        isEqpAlarm: 1,
        lotId: 'T1234567-01',
        recipeName: '001',
        loadPortId: '1',
        chamberId: 'A,B',
        productId: 'GH1832',
        carrierId: 'Carrier-01',
        batchId: 'BatchId-01',
        reserved: '1',
        state: 'Processing',
        step: '1'
      },
      searchTags: Object.freeze([
        { label: '发报警间隔(毫秒)', prop: 'interval', tag: 'DynInputNumber', min: 100, placeholder: ' ' },

        { label: '设备编号', prop: 'eqpId', tag: 'DynInput', placeholder: ' ' },
        { label: '报警开关', prop: 'alarmFlag', tag: 'DynSelect', placeholder: ' ' },
        { label: '报警代码', prop: 'alarmId', tag: 'DynInput', placeholder: ' ' },
        { label: '是否机台报警', prop: 'isEqpAlarm', tag: 'DynSelect', placeholder: ' ' },
        { label: '开始时间', prop: 'startTime', tag: 'Dyndate', placeholder: ' ' },
        { label: '结束时间', prop: 'endTime', tag: 'Dyndate', placeholder: ' ' },
        { label: '报警描述', prop: 'alarmDesc', tag: 'DynTextarea', placeholder: ' ', row: 1, className: 'test' },

        { label: 'lotId', prop: 'lotId', tag: 'DynInput', placeholder: ' ' },
        { label: 'recipeName', prop: 'recipeName', tag: 'DynInput', placeholder: ' ' },
        { label: 'loadPortId', prop: 'loadPortId', tag: 'DynInput', placeholder: ' ' },
        { label: 'chamberId', prop: 'chamberId', tag: 'DynInput', placeholder: ' ' },
        { label: 'productId', prop: 'productId', tag: 'DynInput', placeholder: ' ' },
        { label: 'carrierId', prop: 'carrierId', tag: 'DynInput', placeholder: ' ' },
        { label: 'batchId', prop: 'batchId', tag: 'DynInput', placeholder: ' ' },
        { label: 'reserved', prop: 'reserved', tag: 'DynInput', placeholder: ' ' },
        { label: 'state', prop: 'state', tag: 'DynInput', placeholder: ' ' },
        { label: 'step', prop: 'step', tag: 'DynInput', placeholder: ' ' }
      ]),
      selectList: Object.freeze({
        alarmFlag: [{ id: 0, name: '关' }, { id: 1, name: '开' }],
        isEqpAlarm: [{ id: 0, name: '关' }, { id: 1, name: '开' }]
      }),
      rules: {
        eqpId: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('eqpName')}`, trigger: 'blur' }],
        alarmId: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('alarmId')}`, trigger: 'blur' }]
      },
      messageList: [],
      messageStyles: {
        transform: 'translate(0, 0px)',
        transition: 'all 0s ease-out'
      }
    }
  },
  destroyed() {
    clearTimeout(this.timer)
  },
  methods: {
    send() {
      this.$refs['alarmBaseForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          sendAlarm(this.$clearNull(this.searchForm)).then(res => {
            const { year, mouth, day, hour, minutes, seconds } = getFullTime()
            if (res.code === 1) {
              if (this.messageList.length < 5) {
                this.messageList.push({ id: count++, time: `${year}-${mouth}-${day} ${hour}:${minutes}:${seconds}`, content: '发送成功!' })
              } else {
                this.messageList = this.messageList.concat([{ id: count++, time: `${year}-${mouth}-${day} ${hour}:${minutes}:${seconds}`, content: '发送成功!' }]).slice(1)
                this.autoScroll()
              }

              this.timer = setTimeout(() => {
                this.send()
              }, this.searchForm.interval || 1000)
            }
          })
        }
      })
    },
    cancelSend() {
      clearTimeout(this.timer)
      this.messageStyles = {
        transform: 'translate(0, 0px)',
        transition: 'all .5s ease-out'
      }
    },
    autoScroll() {
      if (this.messageList.length < 5) return
      this.messageStyles = {
        transform: 'translate(0, 0px)',
        transition: 'all 0s ease-out'
      }
      setTimeout(() => {
        this.messageStyles = {
          transform: 'translate(0, -40px)',
          transition: 'all .5s ease-out'
        }
      }, 500)
    }
  }
}
</script>

<style scoped lang="scss">
@import "~@/assets/styles/defaultStyle.scss";
.alarm-testTool{
    padding-bottom: 40px;
}
.alarm-eqp{
    width: 100%;
    background-color: #fff;
    padding: 20px;
    .lotinfo-tilte{
        font-size: 16px;
        font-weight: bold;
    }
}
.alarm-base{
  width: 100%;
    background-color: #fff;
    padding: 0 20px 20px 20px;
    .lotinfo-tilte{
        font-size: 16px;
        font-weight: bold;
    }
}
.alarm-lotinfo {
    width: 100%;
    background-color: #fff;
    padding: 20px;
    .lotinfo-tilte{
        font-size: 16px;
        font-weight: bold;
    }
}
.swiper-message {
  width: 100%;
  margin-top: 20px;
  background-color: #fff;
  border: 1px solid #e6ebf5;
  border-radius: 4px;
  box-sizing: border-box;
  padding: 4px 10px;
  .swiper-title{
    width: 100%;
    display: flex;
    justify-content: flex-start;
    text-align: center;
    height: 30px;
    line-height: 30px;
    border-bottom: 1px solid #ccc;
    .swiper-title-time{
      flex: 1;
    }
    .swiper-title-message{
      flex: 4;
    }
  }
  .swiper-content {
    width: 100%;
    height: 200px;
    overflow-x: hidden;
    overflow-y: auto;
    .swiper-message-item {
      display: flex;
      justify-content: flex-start;
      text-align: center;
      height: 40px;
      line-height: 40px;
      font-size: 14px;
      .item-time{
        flex: 1;
      }
      .item-message{
        flex: 4;
        display: inline-block;
      }
    }
  }
}
.alarm-tools-box{
    width: 100%;
    .alarm-status-title{
        width: 100%;
        display: flex;
        .alarm-status-info{
            font-size: 14px;
            padding: 4px 0;
        }
        .alarm-status-time{
            flex: 1;
            text-align: center;
            border-right: 1px solid #ccc;
        }
        .alarm-status-message{
            flex: 3;
            text-align: center;
        }
    }
}
</style>
